﻿using System.Linq.Expressions;
using dpa.Library.Helper;
using dpa.Library.Models;
using SQLite;

namespace dpa.Library.Services;

public class PoetryStorage(IPreferenceStorage preferenceStorage) : IPoetryStorage
{
    public const int NumberPoetry = 30;

    private const string DbName = "poetrydb.sqlite3";

    public static readonly string PoetryDbPath = PathHelper.GetLocalFilePath(DbName);

    private SQLiteAsyncConnection _connection;

    private SQLiteAsyncConnection Connection => _connection ??= new SQLiteAsyncConnection(PoetryDbPath);

    public bool IsInitialized => preferenceStorage.Get(PoetryStorageConstant.VersionKey, default(int)) ==
                                 PoetryStorageConstant.Version;

    public async Task InitializeAsync()
    {
        await using var dbFileStream = new FileStream(PoetryDbPath, FileMode.OpenOrCreate);

        await using var dbAssetStream = typeof(PoetryStorage).Assembly.GetManifestResourceStream(DbName);

        await dbAssetStream.CopyToAsync(dbFileStream);

        preferenceStorage.Set(PoetryStorageConstant.VersionKey, PoetryStorageConstant.Version);
    }

    public async Task<Poetry> GetPoetryAsync(int id) =>
        await Connection.Table<Poetry>().FirstOrDefaultAsync(p => p.Id == id);

    public async Task<IEnumerable<Poetry>> GetPoetriesAsync(
        Expression<Func<Poetry, bool>> where, int skip, int take) =>
        await Connection.Table<Poetry>().Where(where).Skip(skip).Take(take)
            .ToListAsync();

    public async Task CloseAsync() => await Connection.CloseAsync();
}

public static class PoetryStorageConstant
{
    public const string VersionKey =
        nameof(PoetryStorageConstant) + "." + nameof(VersionKey);
    // nameof(PoetryStorageConstant) -> "PoetryStorageConstant"
    // "PoetryStorageConstant.DbVersionKey"

    public const int Version = 1;
}